//
//  RHUserGroupListVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/11/13.
//

#import "RHUserGroupListVC.h"
#import "RHAddPlantViewController.h"
#import "RHUserGroupDetailVC.h"
#import "RHUserGroupCell.h"
#import "RHPlantTransferVC.h"
#import "KWTPlantGroupDetailVC.h"
#import "RHEditUserGroupVC.h"
#import "KWTS40KVPPSetVC.h"
#import "KWT233VPPSetVC.h"
#import "IFMMenu.h"
#import "RHGolobalSetModel.h"
#import "SYChartTool.h"
#import "KWTMGRNSettingModel.h"
#import "KWT233KSettingModel.h"

@interface RHUserGroupListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *userGroupListArr;

@property (nonatomic, assign) RequestType requestType;
//当前页数
@property (nonatomic, assign) NSInteger currentPage;
//总页数
@property (nonatomic, assign) NSInteger totalPage;
//每页多少条数据
@property (nonatomic, assign) NSInteger numOfPage;

@property (nonatomic ,copy) NSString *keyCode;

@end

@implementation RHUserGroupListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _userGroupListArr = [NSMutableArray array];
    self.view.backgroundColor = CFTabViewBgColor;
    self.navigationItem.title = @"电站分组".rh_localized;
    self.requestType = RequestTypeRefresh;
    self.currentPage = 1;
    self.numOfPage = 20;
    [self setupUI];
    [self getUserGroupList];
}

- (void)setupUI{
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getUserGroupList) name:UserGroupListNeedUpdateNotification object:nil];
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setBackgroundColor:UIColor.clearColor];
    [_tableView registerClass:[RHUserGroupCell class] forCellReuseIdentifier:@"RHUserGroupCell"];
    [self.view addSubview:_tableView];
    _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"没有数据".rh_localized detailStr:@""];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    kWeakSelf;
    [_tableView addHeaderRefresh:^{
        [weakSelf.tableView.mj_footer resetNoMoreData];
        weakSelf.requestType = RequestTypeRefresh;
        weakSelf.currentPage = 1;
        weakSelf.keyCode = @"";
        [weakSelf getUserGroupList];
        //刷新个数
//        if(weakSelf.refreshBlock){
//            weakSelf.refreshBlock();
//        }
    }];
    
    [_tableView addBackFooterRefresh:^{
        weakSelf.requestType = RequestTypeAdd;
        weakSelf.currentPage++;
        [weakSelf getUserGroupList];
    }];
    
}

-(void)searchGroupListWithKeyCode:(NSString*)keyCode
{
    self.currentPage = 1;
    self.requestType = RequestTypeRefresh;
    [self.tableView endFooterResetNoMoreData];
    self.keyCode = keyCode;
    [self getUserGroupList];
}

#pragma mark ---------- 获取用户分组列表 -----------
-(void)getUserGroupList
{
    
        //获取自己的电站列表
        NSDictionary *para = @{@"pageNum":@(self.currentPage),
                               @"pageSize":@(self.numOfPage),
                               @"groupName":ValidNullData(self.keyCode)
        };
        [RHUserGroupModel startGetWithCompletionAPIKey:H_userGroupList argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            if(resultModel.code == 200){
                NSInteger total = [request.responseObject[@"total"] integerValue];
                self.totalPage = total%self.numOfPage? total/self.numOfPage+1 : total/self.numOfPage;
                NSMutableArray *arr = [NSMutableArray arrayWithArray:resultModel.data];
                //下拉刷新
                if(self.requestType == RequestTypeRefresh){
                    [self.tableView endHeaderRefresh];
                    self.userGroupListArr = arr;
                }else{//上拉加载
                    [self.tableView endFooterRefresh];
                    [self.userGroupListArr addObjectsFromArray:arr];
                }
                if(self.currentPage >= self.totalPage){
                    [self.tableView endFooterRefreshWithNoMoreData];
                }
                [self.tableView reloadData];
                if(self.userGroupListArr.count == 0){
                    [self.tableView endFooterRefreshWithNoMoreData];
                }
            }

        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            [QMUITips hideAllToastInView:self.view animated:YES];
            [self.tableView endHeaderRefresh];
        }];
    
    
    
}



#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.userGroupListArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RHUserGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHUserGroupCell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.model = self.userGroupListArr[indexPath.row];
   
    kWeakSelf;
    
    MGSwipeButton *oneBtn = [MGSwipeButton buttonWithTitle:@"   " icon:[UIImage imageNamed:@"plant_delete"] backgroundColor:CFTabViewBgColor padding:15 callback:^BOOL(MGSwipeTableCell * _Nonnull cell) {
        QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"取消".rh_localized style:QMUIAlertActionStyleCancel handler:NULL];
        QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"确定".rh_localized style:QMUIAlertActionStyleDestructive handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
            [weakSelf deleteUserGroupWithIndex:indexPath.row];
        }];
        QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"删除此用户分组?".rh_localized message:nil preferredStyle:QMUIAlertControllerStyleAlert];
        [alertController addAction:action1];
        [alertController addAction:action2];
        [alertController showWithAnimated:YES];
        
        return YES;
    }];
    //终端用户才有删除功能
    cell.rightButtons = @[oneBtn];
  
    cell.moreBlock = ^(RHUserGroupModel * _Nonnull model,CGRect btnRect) {
        [weakSelf showMenu:model rect:btnRect];
    };
    //强制充电
    cell.chgBlock = ^(RHUserGroupModel * _Nonnull model) {
        [weakSelf getplantGroupGolobalConfig:model type:1];
    };
    //强制放电
    cell.dchgBlock = ^(RHUserGroupModel * _Nonnull model) {
        [weakSelf getplantGroupGolobalConfig:model type:2];
    };
    return cell;
}

-(void)showMenu:(RHUserGroupModel*)model rect:(CGRect)rect
{
    kWeakSelf;
    NSMutableArray *menuItems = [NSMutableArray array];
    NSArray *titles = @[@"编辑".rh_localized,@"VPP设置".rh_localized];
    NSArray *images = @[@"vpp_edit",@"vpp_set"];
    NSArray *actions = @[@"editUserGroup:",@"setUserGroup:"];
    __block CGFloat itemWidth = 0;
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
      IFMMenuItem *item = [IFMMenuItem itemWithImage:[UIImage imageNamed:images[idx]]
                             title:title
                            action:^(IFMMenuItem *item) {
          SEL selector = NSSelectorFromString(actions[idx]);
          if ([weakSelf respondsToSelector:selector]) {
              [weakSelf performSelector:selector withObject:model];
          }
        }];
        [menuItems addObject:item];
        CGFloat width = [title getStringWidthFont:kSYSTEMFONT(15) limitHeight:20];
        itemWidth = MAX(width, itemWidth);
    }];
     

    IFMMenu *menu = [[IFMMenu alloc] initWithItems:menuItems];
    menu.menuCornerRadiu = 8;
    menu.showShadow = YES;
    menu.minMenuItemHeight = 45;
    menu.titleColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    menu.titleFont = [UIFont systemFontOfSize:15];
    menu.menuBackgroundStyle = IFMMenuBackgroundStyleLight;
    [menu showFromRect:CGRectMake(kUI_WINDOW_W-itemWidth-25, rect.origin.y, itemWidth+20, 20) inView:kAppWindow];
}

-(void)setUserGroup:(RHUserGroupModel*)model
{
    if (model.userNum == 0) {
        [QMUITips showInfo:@"请添加用户".rh_localized];
        return;
    }
    kWeakSelf;
    if ([model.type isEqualToString:@"S40K LV1.0"]) {
        KWTS40KVPPSetVC *setVC = [[KWTS40KVPPSetVC alloc]init];
        setVC.userGroupModel = model;
        setVC.finishBlock = ^{
            [weakSelf getUserGroupList];
        };
        [self.navigationController pushViewController:setVC animated:YES];
    }
    if ([model.type is233DeviceType]) {
        KWT233VPPSetVC *setVC = [[KWT233VPPSetVC alloc]init];
        setVC.userGroupModel = model;
        setVC.finishBlock = ^{
            [weakSelf getUserGroupList];
        };
        [self.navigationController pushViewController:setVC animated:YES];
    }
}

-(void)editUserGroup:(RHUserGroupModel*)model
{
    RHEditUserGroupVC *editVC = [[RHEditUserGroupVC alloc]init];
    editVC.plantGroupModel = model;
    kWeakSelf;
    editVC.editBlock = ^{
        [weakSelf getUserGroupList];
    };
    [self.navigationController pushViewController:editVC animated:YES];
}

#pragma mark -------s40K 1强制充电  2强制放电
-(void)s40kChgAction:(RHUserGroupModel*)model type:(NSInteger)type configModel:(RHGolobalSetModel*)configModel
{
    if (model.userNum  == 0) {
        [QMUITips showInfo:@"请添加用户".rh_localized];
        return;
    }
  
    NSArray *arr = [SYChartTool loadJsonFileWithName:@"setting_ao_mgrn_chg"];
    NSArray *modelArr = [NSArray yy_modelArrayWithClass:[KWTMGRNSettingModel class] json:arr];
    
    NSMutableArray *paraArr = [NSMutableArray array];
    NSDictionary *modeDic = @{
        @"paramName": @"Inv3_AdvaMode",
        @"regAddr" : @(1067),
        @"offset" : @(0),
        @"num" : @(16),
        @"dataType" : @"int",
        @"value" : @"1"

    };

    NSDictionary *touDic = @{
        @"paramName": @"Inv3_TOUEn",
        @"regAddr" : @(1069),
        @"offset" : @(4),
        @"num" : @(1),
        @"dataType" : @"int",
        @"value" : @"1"

    };
    [paraArr addObject:modeDic];
    [paraArr addObject:touDic];
    [modelArr enumerateObjectsUsingBlock:^(KWTMGRNSettingModel*  _Nonnull sectionModel, NSUInteger sectionIdx, BOOL * _Nonnull stop) {
        [sectionModel.rows enumerateObjectsUsingBlock:^(KWTMGRNSettingModel*  _Nonnull rowModel, NSUInteger rowIdx, BOOL * _Nonnull stop) {
            if (![rowModel.type isEqualToString:@"time"]) {//只处理时间设置
                return;
            }
            [rowModel.items enumerateObjectsUsingBlock:^(KWTMGRNSettingModel * _Nonnull itemModel, NSUInteger itemIdx, BOOL * _Nonnull stop) {
                NSMutableDictionary *textDic = [NSMutableDictionary dictionary];
                [textDic setObject:itemModel.format forKey:@"dataType"];
                [textDic setObject:itemModel.key forKey:@"paramName"];
                
                [textDic setObject:@(itemModel.regAddr) forKey:@"regAddr"];
                [textDic setObject:@(itemModel.num) forKey:@"num"];
                [textDic setObject:@(itemModel.offset) forKey:@"offset"];
                
                NSString *ts_key = [NSString stringWithFormat:@"Inv3_TS%zd",rowIdx+1];
                NSString *td_key = [NSString stringWithFormat:@"Inv3_TD%zd",rowIdx+1];
                NSString *soc_key = [NSString stringWithFormat:@"Inv3_TSoc%zd",rowIdx+1];
                NSString *power_key = [NSString stringWithFormat:@"Inv3_TPo%zd",rowIdx+1];
                NSString *chg_key = [NSString stringWithFormat:@"Inv3_T%zdEn",rowIdx+1];
                NSString *dchg_key = [NSString stringWithFormat:@"Inv3_Gen%zdEn",rowIdx+1];
                if ([itemModel.key isEqualToString:ts_key]) {
                    [textDic setObject:@"0000" forKey:@"value"];
                }
                if ([itemModel.key isEqualToString:td_key]) {//时段1以下的都设成0
                    [textDic setObject:rowIdx==0?@"2359":@"0000" forKey:@"value"];
                }
                
                if ([itemModel.key isEqualToString:soc_key]) {
                    NSString *soc = type == 1?ValidNullData(configModel.chargeSoc):ValidNullData(configModel.dischargeSoc);
                    if (rowIdx>0) {
                        soc = @"0";
                    }
                    [textDic setObject:soc forKey:@"value"];
                }
                
                if ([itemModel.key isEqualToString:power_key]) {
                    NSString *power = ValidNullData(configModel.power);
                    
                    if ([configModel.power integerValue]>30000) {
                        power = @"30000";
                    }
                    if (rowIdx > 0) {
                        power = @"0";
                    }
                    [textDic setObject:power forKey:@"value"];
                }
                
                if ([itemModel.key isEqualToString:chg_key]) {
                    [textDic setObject:type==1?@"1":@"0" forKey:@"value"];
                }
                
                if ([itemModel.key isEqualToString:dchg_key]) {
                    [textDic setObject:type==1?@"0":@"1" forKey:@"value"];
                }
                [paraArr addObject:textDic];
            }];
        }];
    }];
    
    
    //参数组装完
    NSMutableDictionary *requestPara = [NSMutableDictionary dictionary];
    
    //分组id
    [requestPara setObject:@(model.groupId) forKey:@"groupId"];
    
    //参数的body
    NSMutableDictionary *bodyDic = [NSMutableDictionary dictionary];
    NSMutableArray *paraListArr = [NSMutableArray array];
    
    //全部参数一次性给到后台 , 不需要填端点sn参数,因为获取不到
    [paraListArr addObjectsFromArray:paraArr];

    if (paraListArr.count == 0) {
        return;
    }
    [bodyDic setObject:paraListArr forKey:@"commandParamList"];
    [requestPara setValue:bodyDic forKey:@"protocolBody"];
    
    //参数的头部
    NSString *randomStr = [NSString generateRandomLetter:15];
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]*1000;
    NSDictionary *headContent = @{
        @"cmdId": randomStr,
       
        @"paramCount":@(paraListArr.count),
       
        @"messageType": @(1),//下行
        @"namespace": @"ParamSetSingle",
        @"payloadVersion": @(1),
        @"timestamp": @((NSInteger)interval)
      };
    [requestPara setValue:headContent forKey:@"protocolHead"];
    
    [QMUITips showLoading:@"设置中...".rh_localized inView:kApplication.keyWindow];
    [BaseModel startWithCompletionAPIKey:H_userGroupVppSet argument:requestPara requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {
            [QMUITips showSucceed:@"设置完成".rh_localized inView:self.view hideAfterDelay:1];
        }
        
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
    
    }];
}


#pragma mark -------233 1强制充电  2强制放电
-(void)me233ChgAction:(RHUserGroupModel*)model type:(NSInteger)type configModel:(RHGolobalSetModel*)configModel
{
    if (model.userNum == 0) {
        [QMUITips showInfo:@"请添加用户".rh_localized];
        return;
    }
    
    NSArray *arr = [SYChartTool loadJsonFileWithName:@"setting_me_233_vpp_set"];
    NSArray *modelArr = [NSArray yy_modelArrayWithClass:[KWT233KSettingModel class] json:arr];
    
    //生成24个时间
    NSMutableArray *rowArr = [NSMutableArray array];
    [modelArr enumerateObjectsUsingBlock:^(KWT233KSettingModel*  _Nonnull sectionModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([sectionModel.type isEqualToString:@"time"]) {
            KWT233KSettingModel *rowModel = [sectionModel.rows firstObject];
            KWT233KSettingModel *socModel = [rowModel.items safeObjectAtIndex:1];
            KWT233KSettingModel *powerModel = [rowModel.items safeObjectAtIndex:2];
            for (int i = 0; i <24; i++) {
                KWT233KSettingModel *rowNewModel = [[KWT233KSettingModel alloc]init];
                rowNewModel.type = rowModel.type;
                rowNewModel.comment = [NSString stringWithFormat:@"%02d:00-%02d:00",i,i+1];
                
                NSMutableArray *items = [NSMutableArray array];

                KWT233KSettingModel *socNewModel = [[KWT233KSettingModel alloc]init];
                socNewModel.key     = [NSString stringWithFormat:@"%@%d",socModel.key,i+1];
                socNewModel.format  = socModel.format;
                socNewModel.type    = socModel.type;
                socNewModel.regAddr = socModel.regAddr + i;
                socNewModel.offset  = socModel.offset;
                socNewModel.num     = socModel.num;
                KWT233KSettingModel *powerNewModel = [[KWT233KSettingModel alloc]init];
                powerNewModel.key     = [NSString stringWithFormat:@"%@%d",powerModel.key,i+1];
                powerNewModel.format  = powerModel.format;
                powerNewModel.type    = powerModel.type;
                powerNewModel.regAddr = powerModel.regAddr + i;
                powerNewModel.offset  = powerModel.offset;
                powerNewModel.num     = powerModel.num;
//                [items addObject:chgTagNewModel];
                [items addObject:socNewModel];
                [items addObject:powerNewModel];
                rowNewModel.items = items.copy;
                [rowArr addObject:rowNewModel];
            }
            sectionModel.rows = rowArr.copy;
        }
    }];
    
    
    NSMutableArray *paraArr = [NSMutableArray array];
    [modelArr enumerateObjectsUsingBlock:^(KWT233KSettingModel*  _Nonnull sectionModel, NSUInteger idx, BOOL * _Nonnull stop) {
       
        [sectionModel.rows enumerateObjectsUsingBlock:^(KWT233KSettingModel * _Nonnull rowModel, NSUInteger idx, BOOL * _Nonnull stop) {
            //系统设置不提交(单独提交)
            if ([rowModel.type isEqualToString:@"date"]) {
                return;
            }
            //工作模式
            if ([rowModel.type isEqualToString:@"switch"]||[rowModel.type isEqualToString:@"select"]) {
                if (rowModel.content.length == 0) {
                    return;
                }
                NSMutableDictionary *textDic = [NSMutableDictionary dictionary];
                [textDic setObject:rowModel.format forKey:@"dataType"];
                [textDic setObject:rowModel.key forKey:@"paramName"];
                [textDic setObject:rowModel.content forKey:@"value"];//默认削峰填谷
                [textDic setObject:@(rowModel.regAddr) forKey:@"regAddr"];
                [textDic setObject:@(rowModel.num) forKey:@"num"];
                [textDic setObject:@(rowModel.offset) forKey:@"offset"];
                [paraArr addObject:textDic];
            }
            
            //时间段设置
            if ([rowModel.type isEqualToString:@"time"]) {
               
                [rowModel.items enumerateObjectsUsingBlock:^(KWT233KSettingModel * _Nonnull itemModel, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (itemModel.key.length == 0) {//充放电字段不提交
                        return;
                    }
                    NSMutableDictionary *itemDic = [NSMutableDictionary dictionary];
                    [itemDic setObject:itemModel.format forKey:@"dataType"];
                    [itemDic setObject:itemModel.key forKey:@"paramName"];
//                    [itemDic setObject:itemModel.content forKey:@"value"];
                    [itemDic setObject:@(itemModel.regAddr) forKey:@"regAddr"];
                    [itemDic setObject:@(itemModel.num) forKey:@"num"];
                    [itemDic setObject:@(itemModel.offset) forKey:@"offset"];
                    
                    if ([itemModel.key containsString:@"Emu_EndSocSeg"]) {//SOC
                        NSString *socValue = type == 1?configModel.chargeSoc:configModel.dischargeSoc;
                        [itemDic setObject:socValue forKey:@"value"];
                    }
                    //功率值要根所充放电状态来发送正负值
                    if ([itemModel.key containsString:@"Emu_PwrSeg"]) {//功率
                        NSString *value = configModel.power;
                        if ([value integerValue]>125) {//最大功率100KW
                            value = @"125";
                        }
                        if (type == 1) {//充电发正数
                           
                            [itemDic setObject:value forKey:@"value"];
                        }
                        if (type == 2) {//放电发负数
                            if (![value containsString:@"-"]) {
                                NSString *dchg = [NSString stringWithFormat:@"-%@",value];
                                [itemDic setObject:dchg forKey:@"value"];
                            }
                        }
                    }
                    [paraArr addObject:itemDic];
                }];
            }
            
        }];
        
    }];
    
    //组装好参数后
    NSMutableDictionary *requestPara = [NSMutableDictionary dictionary];
    
    //分组id
    [requestPara setObject:@(model.groupId) forKey:@"groupId"];
    
    
    //参数的body
    NSMutableDictionary *bodyDic = [NSMutableDictionary dictionary];
    NSMutableArray *paraListArr = [NSMutableArray array];
    
    //全部参数一次性给到后台 , 不需要填端点sn参数,因为获取不到
    [paraListArr addObjectsFromArray:paraArr];

    if (paraListArr.count == 0) {
        return;
    }
    [bodyDic setObject:paraListArr forKey:@"commandParamList"];
    [requestPara setValue:bodyDic forKey:@"protocolBody"];
    
    //参数的头部
    NSString *randomStr = [NSString generateRandomLetter:15];
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]*1000;
    NSDictionary *headContent = @{
        @"cmdId": randomStr,
        
        @"paramCount":@(paraListArr.count),
      
        @"messageType": @(1),//下行
        @"namespace": @"ParamSetSingle",
        @"payloadVersion": @(1),
        @"timestamp": @((NSInteger)interval)
      };
    [requestPara setValue:headContent forKey:@"protocolHead"];
    
    [QMUITips showLoading:@"设置中...".rh_localized inView:kApplication.keyWindow];

    [BaseModel startWithCompletionAPIKey:H_userGroupVppSet argument:requestPara requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {

            [QMUITips showSucceed:@"设置完成".rh_localized inView:self.view hideAfterDelay:1];
            
        }
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
      
    }];
}
#pragma mark ----- 获取一键配置
-(void)getplantGroupGolobalConfig:(RHUserGroupModel*)model type:(NSInteger)setType
{
    [QMUITips showLoadingInView:kAppWindow];
    kWeakSelf;
    [RHGolobalSetModel startGetWithCompletionAPIKey:H_getPlantGroupForceConfig argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {
            NSDictionary *dataDic = request.responseJSONObject[@"data"];
            if ([model.type isEqualToString:@"S40K LV1.0"]) {
                if (dataDic[model.type]) {
                    RHGolobalSetModel *s40SetModel = [RHGolobalSetModel yy_modelWithJSON:dataDic[model.type]];
                    [weakSelf s40kChgAction:model type:setType configModel:s40SetModel];
                }else{
                    [QMUITips showInfo:@"请先配置充放电参数".rh_localized];
                }
               
            }
            if ([model.type is233DeviceType]) {
                if (dataDic[model.type]) {
                    RHGolobalSetModel *me233SetModel = [RHGolobalSetModel yy_modelWithJSON:dataDic[model.type]];
                    [weakSelf me233ChgAction:model type:setType configModel:me233SetModel];
                }else{
                    [QMUITips showInfo:@"请先配置充放电参数".rh_localized];
                }
               
            }
        }
        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            
        }];
    
}

#pragma mark ------- 删除用户分组 -------
-(void)deleteUserGroupWithIndex:(NSInteger)index
{
    RHUserGroupModel *model = self.userGroupListArr[index];
    NSString *url = [NSString stringWithFormat:@"user/groups/%ld",model.groupId];
    [QMUITips showLoadingInView:kApplication.keyWindow];
    [BaseModel startDeleteWithCompletionAPIKey:url argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
            [self.userGroupListArr removeObjectAtIndex:index];
            [self.tableView reloadData];
            [QMUITips showSucceed:@"成功".rh_localized inView:self.view hideAfterDelay:1];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self getUserGroupList];
            });
            
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RHUserGroupModel *model = [self.userGroupListArr safeObjectAtIndex:indexPath.row];
    RHUserGroupDetailVC *vc = [[RHUserGroupDetailVC alloc] init];
    vc.title = model.groupName;
    vc.groupModel = model;
    [self.navigationController pushViewController:vc animated:YES];
   
    
}

#pragma mark - JXCategoryListContentViewDelegate
/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}

-(void)dealloc
{
   
}

@end
